﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Widgetsphere.Autos.EFDAL;
using Widgetsphere.Autos.EFDAL.Entity;

namespace Widgetsphere.Autos.API
{
    public static class ConfigHelper
    {
        private static DateTime _lastUpdate = DateTime.MinValue;
        private static List<ConfigSetting> _settings = null;
        private static readonly object _syncObject = new object();

        #region Constructors

        static ConfigHelper()
        {
            Refresh();
            try
            {
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public static void Refresh()
        {
            try
            {
                lock (_syncObject)
                {
                    //Load all settings from database
                    using (var context = new AutosEntities())
                    {
                        _settings = context.ConfigSetting.ToList();
                        _lastUpdate = DateTime.Now;
                    }
                }

            }
            catch (Exception ex)
            {
                //Logger.LogWarning("ConfigHelper.Refresh", ex);
            }
        }

        public static void SetSetting(int id, string value)
        {
            lock (_syncObject)
            {
                using (var context = new AutosEntities())
                {
                    var item = context.ConfigSetting.FirstOrDefault(x => x.ID == id);
                    if (item != null)
                    {
                        item.Value = value;
                        context.SaveChanges();
                    }
                }
            }
            Refresh();
        }

        #endregion

        #region Setting Methods

        public static IEnumerable<ConfigSetting> AllSettings
        {
            get
            {
                lock (_syncObject)
                {
                    if (DateTime.Now.Subtract(_lastUpdate).TotalSeconds > 20)
                    {
                        Refresh();
                    }
                    return _settings;
                }
            }
        }

        private static string GetValue(string name)
        {
            return GetValue(name, string.Empty);
        }

        private static string GetValue(string name, string defaultValue)
        {
            try
            {
                var q = AllSettings.FirstOrDefault(x => x.Name.ToLower() == name.ToLower());
                if (q == null) return defaultValue;
                else return q.Value;

            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "Name=" + name + ", DefaultValue=" + defaultValue);
                throw;
            }
        }

        private static int GetValue(string name, int defaultValue)
        {
            int retVal;
            if (int.TryParse(GetValue(name, string.Empty), out retVal))
                return retVal;
            return defaultValue;
        }

        private static bool GetValue(string name, bool defaultValue)
        {
            bool retVal;
            if (bool.TryParse(GetValue(name, string.Empty), out retVal))
                return retVal;
            return defaultValue;
        }

        #endregion

        #region Properties

        public static string DatastoreServer
        {
            get { return GetValue("DatastoreServer", "localhost"); }
        }

        public static int DatastorePort
        {
            get { return GetValue("DatastorePort", 1973); }
        }

        public static bool AllowMaps
        {
            get { return GetValue("AllowMaps", true); }
        }

        public static Dictionary<string, string> JobDimensionAlias
        {
            get
            {
                var retval = new Dictionary<string, string>();
                var v = GetValue("JobDimensionAlias", string.Empty);
                var arr = v.Split('|');
                foreach (var s in arr)
                {
                    var arr2 = s.Split(':');
                    if (arr2.Length == 2)
                    {
                        retval.Add(arr2[0], arr2[1]);
                    }
                }
                return retval;
            }
        }

        public static string JobDimensionOrder
        {
            get { return GetValue("JobDimensionOrder", string.Empty); }
        }

        public static bool AllowZipSearch
        {
            get { return GetValue("AllowZipSearch", true); }
        }

        public static string LocationServiceUrl
        {
            get { return GetValue("LocationServiceUrl", string.Empty); }
        }

        #endregion

    }
}